package com.qkm.xinguan.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qkm.xinguan.constant.SystemConst;
import com.qkm.xinguan.domain.entity.Notice;
import com.qkm.xinguan.domain.entity.Tourist;
import com.qkm.xinguan.domain.entity.User;
import com.qkm.xinguan.domain.infrastructure.mapper.TouristMapper;
import com.qkm.xinguan.domain.infrastructure.utils.SpringComponentUtil;
import com.qkm.xinguan.domain.infrastructure.utils.UserUtil;
import com.qkm.xinguan.domain.transform.dto.LoginDTO;
import com.qkm.xinguan.domain.transform.dto.TouristLoginDTO;
import com.qkm.xinguan.domain.transform.dto.UpdateTouristDTO;
import com.qkm.xinguan.domain.transform.form.TouristSearchForm;
import com.qkm.xinguan.domain.transform.form.UserSearchForm;
import com.qkm.xinguan.exception.BusinessException;
import com.qkm.xinguan.repository.TouristRepository;
import com.qkm.xinguan.response.Result;
import com.qkm.xinguan.response.ResultCode;
import com.qkm.xinguan.vo.TouristVo;
import com.qkm.xinguan.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


@Service("touristService")
public class TouristRepositoryImpl extends ServiceImpl<TouristMapper, Tourist> implements TouristRepository {
	@Override
	public boolean checkTouristExist(String username) {
		LambdaQueryWrapper<Tourist> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Tourist::getUsername, username);
		return this.baseMapper.selectCount(wrapper) > 0;
	}

	/**
	 * 检测用户名手机号是否存在
	 *
	 * @param phone 手机号
	 * @return boolean
	 */
	@Override
	public boolean checkPhoneExist(String phone) {
		LambdaQueryWrapper<Tourist> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Tourist::getPhone, phone);
		return this.baseMapper.selectCount(wrapper) > 0;
	}

	@Override
	public Result login(TouristLoginDTO loginDTO) {
		Tourist tourist = this.baseMapper.geByName(loginDTO.getUsername());
		if (null == tourist) {
			return Result.error(ResultCode.Tourist_ACCOUNT_NOT_EXIST);
		}
		if (tourist.getForbidden().equals(SystemConst.FORBIDDEN)) {
			return Result.error(ResultCode.USER_ACCOUNT_LOCKED);
		}
		PasswordEncoder passwordEncoder = SpringComponentUtil.getBean("passwordEncoder", PasswordEncoder.class);
		if (passwordEncoder.matches(loginDTO.getPassword(), tourist.getPassword())) {
			return Result.ok().data(tourist);
		} else {
			return Result.error(ResultCode.USER_CREDENTIALS_ERROR);
		}
	}

	/**
	 * @param id
	 * @param updateTouristDTO
	 * @return
	 */
	@Override
	public Result update(Long id, UpdateTouristDTO updateTouristDTO) {
		Tourist tourist=this.baseMapper.selectById(id);
		if(Objects.isNull(tourist)){
			throw new BusinessException(ResultCode.PARAM_ERROR, "来源信息不存在，请重试！");
		}
		BeanUtils.copyProperties(updateTouristDTO,tourist);
		int res=this.baseMapper.updateById(tourist);
		if(res>0){
			return Result.ok();
		}
		throw new BusinessException(ResultCode.FAILED);
	}

	@Override
	public List<String> getTouristNamesByIds(List<Long> ids) {
		return this.baseMapper.getTouristNamesByIds(ids);
	}

	@Override
	public List<String> getTouristPhonesByIds(List<Long> ids) {
		return this.baseMapper.getTouristPhonesByIds(ids);
	}

	@Override
	public Page<Tourist> getTouristListPage(Integer page, Integer size) {
		Page<Tourist> p = this.baseMapper.selectPage(new Page<>(page, size), null);
		return p;
	}

	@Override
	public Page<Tourist> searchTouristListPage(TouristSearchForm touristSearchForm, Integer page, Integer size) {
		LambdaQueryWrapper<Tourist> wrapper = executeWrapper(touristSearchForm);
		Page<Tourist> pageInfo = this.baseMapper.selectPage(new Page<>(page, size), wrapper);
		return pageInfo;
	}

	@Override
	public int updateTouristStatusById(Long id, Integer status) {
		return this.baseMapper.updateTouristStatusById(id, status);
	}

	private Page<TouristVo> toTouristVOPage(Page<Tourist> touristPage) {
		Page<TouristVo> pageInfo = new Page<>();
		BeanUtils.copyProperties(touristPage, pageInfo);
		List<TouristVo> touristVos = toTouristVoList(touristPage.getRecords());
		pageInfo.setRecords(touristVos);
		return pageInfo;
	}

	private List<TouristVo> toTouristVoList(List<Tourist> tourists) {
		if (tourists.isEmpty()) {
			return Collections.emptyList();
		}
		List<TouristVo> userVos = new ArrayList<>();
		return userVos;
	}

	private static LambdaQueryWrapper<Tourist> executeWrapper(TouristSearchForm touristSearchForm) {
		LambdaQueryWrapper<Tourist> wrapper = new LambdaQueryWrapper<>();
		if (!StringUtils.isEmpty(touristSearchForm.getUsername())) {
			wrapper.like(Tourist::getUsername, touristSearchForm.getUsername());
		}
		if (!StringUtils.isEmpty(touristSearchForm.getForbidden())) {
			wrapper.eq(Tourist::getForbidden, touristSearchForm.getForbidden());
		}
		return wrapper;
	}

}
